package com.utils;

import java.util.Random;


/**
 * 
 * @author yangqifan
 *
 */

public class RandomUtil {
	public static Random random = new Random(); 
	
	/**
	 * getRandom
	 * @return
	 */
	public static Random getRandom(){
		return random;
	}
	
	/** 检测几率 */
	public static boolean checkRate(int rate){
		if(rate <= 0) return false;
		if(rate > 100) rate = 100;
		return random.nextInt(101) < rate ? true : false;
	}
	
	/** 检测几率 */
	public static boolean checkRate(int max, int rate){
		if(max <= 0 || rate <= 0) return false;
		if(rate > max) rate = max;
		return random.nextInt(max + 1) < rate ? true : false;
	}
	
	/**
	 * 返回[0,num]区间的随机数
	 * @param num
	 * @return
	 */
	public static int nextInt(int num){
		if(num < 0) return 0;
		return random.nextInt(num + 1);
	}
	
	/**
	 * 返回[min,max]区间的随机数
	 * if(a < 0 || b < 0) return 0. if(a == b) return a;
	 * @param a
	 * @param b
	 * @return
	 */
	public static int nextInt(int a, int b){
		if(a < 0 || b < 0) return 0;
		if(a == b) return a;
		int min = a <= b ? a : b;
		int max = a >= b ? a : b;
		int temp = max - min;
		int r = random.nextInt(temp + 1);
		return min + r;
	}
	
	/**
	 * 根据一个概率数组返回随机到的数组索引值
	 * @param rates
	 * @return
	 */
	public static int randomIndex(int[] rates){
		int total = 0;
		for(int r : rates){
			total += r;
		}
		
		//在全区间上随机出一个数
		final int rnd = random.nextInt(total + 1);
		int tmp = rnd;
		for(int i = 0; i < rates.length; i++){
			if(tmp <= rates[i]){
				return i;
			}else{
				tmp -= rates[i];
			}
		}
		return 0;
	}
	
	public static int randomIndex(Integer[] rates){
		int total = 0;
		for(int r : rates){
			total += r;
		}
		
		
		final int rnd = random.nextInt(total + 1);
		int tmp = rnd;
		for(int i = 0; i < rates.length; i++){
			if(tmp <= rates[i]){
				return i;
			}else{
				tmp -= rates[i];
			}
		}
		return 0;
	}
	
	/**
	 * double一般是两位小数,这里默认支持两位小数, 更高精度使用{@See nextDouble(..., int dot)}
	 * @param min
	 * @param max
	 * @return
	 */
	public static double nextDouble(double min, double max){
		return nextDouble(min, max, 2);
	}
	
	/**
	 * 自定义精度
	 * @param min
	 * @param max
	 * @param dot 小数点位数
	 * @return
	 */
	public static double nextDouble(double min, double max, int dot){
		double base = Math.pow(10, dot);
		int minInt = (int) (min * base);
		int maxInt = (int) (max * base);
		int nextInt = nextInt(minInt, maxInt);
		
		return nextInt / base;
	}
	
	public static void main(String[] args) {
//		System.out.println(RandomUtil.nextInt(100));
//		System.out.println(RandomUtil.nextInt(1, 10));
//		
		//概率数组测试
		Integer[] rates = {55,20,10,1,1,1};
		int[] counts = new int[rates.length];
		for(int i = 0; i < 100000000; i++){
			int index = randomIndex(rates);
			counts[index] = counts[index] + 1;
		}
		String resut = "[";
		for(int i = 0; i < counts.length; i++){
			resut += counts[i];
			resut += ",";
		}
		resut += "]";
		System.out.println(resut);
		
		
		//double区间取值测试
		double min = 0.6;
		double max = 0.8;
		for(int i = 0; i < 10000000; i++){
			double nextDouble = nextDouble(min, max, 6);
			if(nextDouble < min || nextDouble > max){
				System.out.println(nextDouble);
			}
		}
	}
	
}
